package com.tongyi.lizhishop.activitys;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tongyi.lizhishop.api.UserApi;
import com.tongyi.lizhishop.beans.CommonResonseBean;
import com.tongyi.lizhishop.common.CommonObserver;

import com.tongyi.lizhishop.R;
import com.tongyi.lizhishop.managers.APIServiceManager;

import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ChangePasswordActivity extends FindPasswordActivity {
    public String sencondEtText;
    private String threeEtText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titlebar.getCenterTextView().setText("修改密码");
        rightTopTv.setVisibility(View.INVISIBLE);
        leftTopTv.setText("原密码");
        leftSecondTv.setText("新密码");
        leftThreeTv.setText("重复新密码");
        centerTopEt.setHint("请输入原密码");
        centerSencondEt.setHint("请输入新密码");
        centerThreeEt.setHint("请输入新密码");
        centerSencondEt.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                sencondEtText = s.toString();
                checkBtnEnable();
            }
        });

        centerThreeEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                threeEtText = s.toString();
            }
        });
        centerTopEt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkBtnEnable();
            }
        });
    }

    private void checkBtnEnable() {
        String second = centerSencondEt.getText().toString();
        String three = centerThreeEt.getText().toString();
        if (!TextUtils.isEmpty(centerTopEt.getText().toString())
                && !TextUtils.isEmpty(second)
                && !TextUtils.isEmpty(three)) {
            if (second.equals(three)) {
                confirm.setEnabled(true);
            }
        }
    }

    @OnClick({R.id.confirm})
    @Override
    public void onViewClicked(View view) {

        LogUtils.a("click");
        APIServiceManager.getInstance().create(UserApi.class)
                .changePassword2(SPUtils.getInstance().getString("userid")
                        , centerSencondEt.getText().toString(), centerSencondEt.getText().toString(), centerTopEt.getText().toString())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<CommonResonseBean>() {
                    @Override
                    public void onSuccess(CommonResonseBean value) {
                        if ("succ".equals(value.getRe())) {
                            //密码修改成功
                            ToastUtils.showShort("修改成功");
                            finish();
                        } else if ("oldpassword".equals(value.getRe())) {
                            msg("您输入的旧密码不正确");
                        } else if ("password".equals(value.getRe())) {
                            msg("您输入的两次新密码不正确");
                        }
                    }

                    @Override
                    public void onFailure(Throwable e) {
                        msg("请求失败,请重试");

                    }
                });
    }

    private void msg(String msg) {
        ToastUtils.showShort(msg);
        confirm.reset();
        confirm.setEnabled(true);
    }
}
